package com.test.contorller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.bouncycastle.jcajce.provider.symmetric.AES.Wrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.web.ProjectedPayload;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;
import com.mongodb.client.model.Projections;
import com.test.model.Student;

@RestController
public class StudentContorller {
	
	@Autowired
	MongoTemplate mongoTemplate;
	
	@RequestMapping(value = "/insertStudent")
	public void insertStudent() {
		Student s = new Student(1000,"gxr",13);
		mongoTemplate.insert(s);
		System.out.println("执行完成");
	}
	
	@RequestMapping(value = "/addAll")
	public void insertAllStudent() {
		List<Student> list = new ArrayList<>();
		Student s1 = new Student(10001,"gxr1",13);
		Student s2 = new Student(10002,"gxr2",22);
		list.add(s1);
		list.add(s2);
		mongoTemplate.insertAll(list);
		System.out.println("插入多条记录完成");
	}
	//查询所有
	@RequestMapping(value = "/getAll")
	public List<Student> getAllStudent() {
	
		List<Student> list = mongoTemplate.findAll(Student.class,"student");
		return list;
	}
	
	@RequestMapping(value = "/get/{name}")
	//按条件查询
	public List<Student> findList(@PathVariable String name) {
		Query query = Query.query(Criteria.where("username").is(name));
		List<Student> list = mongoTemplate.find(query, Student.class);
		return list;
	}
	
	@GetMapping(value = "/findQueryGte/{age}")
	//按条件查询 gte >=  gt > lte <= lt <
	public List<Student> findLikeQueryGte(@PathVariable int age){
		Query query = new Query();
		query.addCriteria(Criteria.where("age").gte(age));
		return mongoTemplate.find(query, Student.class);
	}
	
	@GetMapping(value = "/findQueryGt/{age}")
	public List<Student> findLikeQueryGt(@PathVariable int age){
		Query query = new Query();
		query.addCriteria(Criteria.where("age").gt(age));
		return mongoTemplate.find(query, Student.class);
	}
	
	@GetMapping(value = "/findQueryLte/{age}")
	public List<Student> findLikeQueryLte(@PathVariable int age){
		Query query = new Query();
		query.addCriteria(Criteria.where("age").lte(age));
		return mongoTemplate.find(query, Student.class);
	}
	
	@GetMapping(value = "/findQueryLt/{age}")
	public List<Student> findLikeQueryLt(@PathVariable int age){
		Query query = new Query();
		query.addCriteria(Criteria.where("age").lt(age));
		return mongoTemplate.find(query, Student.class);
	}
	
	//模糊查询
	@GetMapping("/findLike")
	public List<Student> findLike2() {
		//select username from table group by username ;
		
		
		return null;
	}
	
	private List project(String string) {
		// TODO Auto-generated method stub
		return null;
	}

	@GetMapping("/update")
	public void updateStudent() {
		Query query = Query.query(Criteria.where("age").is("22")); //更新的条件
		Update update = new Update();
		update.set("user_id", "10003");  //更新的字段
		update.set("username", "gxrtff");
		//updateMulti 更新多条数据
		WriteResult wr = mongoTemplate.updateMulti(query, update,"student");
		
		System.out.println("更新完成");
	}
	
	//有多个更新条件
	@GetMapping(name = "/update2")
	public void updateStudent2() {
		Criteria criteria = new Criteria();
		criteria.where("name").is("zyy");
		criteria.where("age").gt(20);
		Query query = new Query();
		query.addCriteria(criteria);
		Update update = new Update();
		update.set("studentID", "201671010444");
		update.set("username", "tffzyy");
		update.set("age", "28");
	    WriteResult wr = mongoTemplate.updateFirst(query, update, Student.class);
	    System.out.println("unacknowledged = "+wr.unacknowledged());
		System.out.println("更新结果    getN= "+wr.getN()+"  :getUpsertedId=  "+wr.getUpsertedId());
		System.out.println("更新结果    isUpdateOfExisting= "+wr.isUpdateOfExisting()+"  :wasAcknowledged=  "+wr.wasAcknowledged());
		
		
//		unacknowledged = WriteResult{acknowledged=false}
//		更新结果    getN= 1 返回的是更新成功的数量      :getUpsertedId=  null
//		更新结果    isUpdateOfExisting= true  :wasAcknowledged=  true
	}
	
	@GetMapping("/remove")
	public void remove() {
		//Criteria criteria = new Criteria();
		//相当于SQL : delete from student where studentID ='1000' and username='tffzyy';
		//criteria.where("user_id").is("1000");
		//criteria.where("username").is("tffzyy");
		Query query = new Query(new Criteria().where("username").is("jjjj").and("user_id").is(Double.valueOf(10002)));
		//query.addCriteria(criteria);
		WriteResult wr = mongoTemplate.remove(query,Student.class,"student");
		
		System.out.println("删除完成：   "+wr.getN()); 
	}
	
	@GetMapping("/findAgg")
	public List<Student> findAgg() {
		Aggregation agg = Aggregation.newAggregation(
				//Aggregation.match(criteria),
				Aggregation.group("user_id").count().as("username")
				);
		 AggregationResults<Student> outputType =mongoTemplate.aggregate(agg, "student", Student.class);
		List<Student> s = outputType.getMappedResults();
	 return s;
	}
}
